package com.bleeth.flow.web.handler;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.AttachmentProxy;

import java.io.InputStream;

/**
 * 本地上传策略不够灵活，因此 重新设置
 *
 * @author yuepeng
 * @date 2020-05-17
 */
@Service
public class LocalFileUploadProxy implements AttachmentProxy {

    public static String destPath = System.getProperty("user.dir") + "/upload";

    @Override
    public String upLoad(InputStream inputStream, String path) {
        String fileName = FileUtil.getName(path);
        String prefixName = FileUtil.getPrefix(fileName);
        String suffix = FileUtil.getSuffix(fileName);

        prefixName = PinyinUtil.getFirstLetter(prefixName, "");
        prefixName= StrUtil.replace(prefixName,"（","(");
        prefixName= StrUtil.replace(prefixName,"）",")");

        String newName = prefixName + "." + suffix;
        FileUtil.writeFromStream(inputStream, destPath + "/" + newName);
        return "/" + newName;
    }

    @Override
    public boolean isLocalSave() {
        return false;
    }

    @Override
    public String fileDomain() {
        return "http://oos.erupt.xyz";
    }
}